package com.kunya.product.service.impl;

import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.NavigationBarMapper;
import com.kunya.product.dao.NavigationMapper;
import com.kunya.product.po.Navigation;
import com.kunya.product.po.NavigationBar;
import com.kunya.product.service.INavigationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("iNavigationService")
public class NavigationServiceImpl implements INavigationService{

    @Autowired
    private NavigationMapper navigationMapper;
    @Autowired
    private NavigationBarMapper navigationBarMapper;

    /**
     * 导航栏添加分类
     * @param navigation
     * @return
     */
    @Override
    public ServerResponse addNavigation(Navigation navigation){
        if (StringUtils.isEmpty(navigation.getCode())){
            return ServerResponse.createByErrorMessage("代码不能为空!");
        }
        if (StringUtils.isEmpty(navigation.getName())){
            return  ServerResponse.createByErrorMessage("名称不能为空!");
        }
        if (navigation.getStatus() == null){
            return ServerResponse.createByErrorMessage("状态不能为空!");
        }
        if (navigation.getNumber() == null){
            return ServerResponse.createByErrorMessage("数量不能为空!");
        }
        if (navigation.getShowNumber() == null){
            return ServerResponse.createByErrorMessage("显示数量不能为空!");
        }
        /**
         * 设置当前时间
         */
        navigation.setCreateTime(new Date());
        int num = navigationMapper.insertSelective(navigation);
        if (num  <= 0){
            return ServerResponse.createByErrorMessage("添加失败!");
        }else {
            return ServerResponse.createBySuccess("添加成功!");
        }
    }

    /**
     * 显示隐藏导航栏
     * @param navigation
     * @return
     */
    @Override
    public ServerResponse updateNavigation(Navigation navigation){
        if (navigation.getNavigationId() == null){
            return ServerResponse.createByErrorMessage("navigationId必须传入!");
        }
        if (navigation.getStatus() == null){
            return ServerResponse.createByErrorMessage("Status状态必须传入!");
        }
        if (StringUtils.isEmpty(navigation.getUpdateBy())){
            return ServerResponse.createByErrorMessage("更新人必须传入!");
        }
        /**
         * 防止更新其他字段，新建一个类进行操作
         */
        Navigation navigation1 = new Navigation();
        navigation1.setNavigationId(navigation.getNavigationId());
        navigation1.setStatus(navigation.getStatus());
        navigation1.setUpdateTime(new Date());
        navigation1.setUpdateBy(navigation.getUpdateBy());
        int num =    navigationMapper.updateByPrimaryKeySelective(navigation1);
        if (num <= 0){
            return ServerResponse.createByErrorMessage("设置失败!");
        }else {
            return ServerResponse.createBySuccessMessage("设置成功!");
        }
    }

    /**
     * 显示导航栏内的所有商品
     * @param navigationId
     * @return
     */
    @Override
    public ServerResponse showGoods(Long navigationId){
        if (navigationId == null){
            return ServerResponse.createByErrorMessage("navigationId必须传入!");
        }
        return  ServerResponse.createBySuccess(navigationBarMapper.showGoods(navigationId));
    }

    /**
     * 删除导航栏下的单个商品
     * @param navigationBarId
     * @return
     */
    @Override
    public ServerResponse deleteGoods(Long navigationBarId){
        int num = navigationBarMapper.deleteByPrimaryKey(navigationBarId);
        if (num <= 0){
            return ServerResponse.createByErrorMessage("删除失败");
        }else {
            return ServerResponse.createBySuccess("删除成功");
        }
    }

    /**
     * 删除整个导航信息
     * @param navigationId
     * @return
     */
    @Override
    @Transactional
    public ServerResponse deleteNavigation(Long navigationId){
        int num = navigationMapper.deleteByPrimaryKey(navigationId);
        if(num == 0){
            throw new RuntimeException("删除失败");
        }
        List<NavigationBar> list = navigationBarMapper.showGoods(navigationId);
        for (NavigationBar navigationBar:list){
            Long navigationBarId = navigationBar.getNavigationBarId();
           int number = navigationBarMapper.deleteByPrimaryKey(navigationBarId);
            if(number  == 0){
                throw new RuntimeException("删除失败");
            }
        }

        return ServerResponse.createBySuccessMessage("删除成功");
    }

}
